package com.zmn.plat.common.dto;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.utils.collection.CollectionUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 数据权限传输对象
 *
 * @author heguanghua
 * @since 2019/11/26 01:31
 */
public class PermitDTO<T> implements Serializable {
    private static final long serialVersionUID = -3787552820653518129L;

    /** 无权限 */
    public final static int RANGE_EMPTY = 1;
    /** 全部权限 */
    public final static int RANGE_ALL = 2;
    /** 正常权限 */
    public final static int RANGE_NORMAL = 3;

    /** 数值结果,如200,404 */
    private int status;

    /** 文本信息 */
    private String message;

    /** 权限范围 */
    private int range;

    /** 数据对象列表 */
    private List<T> items;


    public static <T> PermitDTO<T> success(List<T> items){
        return new PermitDTO(items);
    }

    public static <T> PermitDTO<T> success(List<T> items, String message){
        return new PermitDTO(StatusConsts.STATUS_SUCCESS, items, message);
    }

    public static <T> PermitDTO<T> success(int range, List<T> items){
        return new PermitDTO(StatusConsts.STATUS_SUCCESS, range, items, null);
    }

    public static <T> PermitDTO<T> success(int range, List<T> items, String message){
        return new PermitDTO(StatusConsts.STATUS_SUCCESS, range, items, message);
    }

    /**
     * 所有权限
     *
     * @param <T>
     * @return
     */
    public static <T> PermitDTO<T> successAll(String message){
        return new PermitDTO(StatusConsts.STATUS_SUCCESS, RANGE_ALL, null, message);
    }

    public static PermitDTO fail(){
        return new PermitDTO(StatusConsts.STATUS_ERROR, null, null);
    }

    public static PermitDTO fail(String message){
        return new PermitDTO(StatusConsts.STATUS_ERROR, null, message);
    }

    public static PermitDTO fail(int status, String message){
        return new PermitDTO(status, null, message);
    }


    /**
     * 构造函数
     */
    public PermitDTO(){super();}

    /**
     * 构造函数，不指定range，根据items判断range值，空列表视为无权限
     * @param items
     */
    public PermitDTO(List<T> items) {
        this.status = StatusConsts.STATUS_SUCCESS;
        this.items = items;
        if (items == null) {
            this.items = Collections.EMPTY_LIST;
        }
        if(this.items.isEmpty()) {
            this.range = RANGE_EMPTY;
        } else {
            this.range = RANGE_NORMAL;
        }
    }

    /**
     * 构造函数，不指定range，则根据items判断range值，空列表视为无权限
     * @param status
     * @param items
     * @param message
     */
    public PermitDTO(int status, List<T> items, String message) {
        this.status = status;
        this.message = message;
        this.items = items;
        if (items == null) {
            this.items = Collections.EMPTY_LIST;
        }
        if(this.items.isEmpty()) {
            this.range = RANGE_EMPTY;
        } else {
            this.range = RANGE_NORMAL;
        }
    }

    /**
     * 构造函数，如明确无数据权限建议使用此构造函数
     * @param status
     * @param range
     * @param items
     * @param message
     */
    public PermitDTO(int status, int range, List<T> items, String message) {
        this.status = status;
        this.message = message;
        this.items = items;
        if (items == null) {
            this.items = Collections.EMPTY_LIST;
        }
        if(range == RANGE_EMPTY && validityCheck(items)) {
            this.range = RANGE_NORMAL;
        } else {
            this.range = range;
        }
    }

    /**
     * 检查数据权限是否有效：不为空，-1检查（无权限会设置值为-1）
     *
     * @param items
     * @return true：有效，false：无效
     */
    private boolean validityCheck(List<T> items) {
        if (items == null) {
            return false;
        }

        if (items.size() == 0) {
            return false;
        }

        T permit = items.get(0);
        boolean checkValue = String.valueOf(permit).equals("-1");
        if (items.size() == 1 && checkValue) {
            return false;
        }

        return true;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getRange() {
        return range;
    }

    public void setRange(int range) {
        this.range = range;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    /**
     * @return 如果成功返回true，否则返回 false。
     */
    public boolean isSuccess() {
        return this.status == StatusConsts.STATUS_SUCCESS;
    }

    @Override
    public String toString() {
        return "PermitDTO{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", range=" + range +
                ", items=" + items +
                '}';
    }
}
